package com.swind.fast.modules.app.space.rent.listener;

import com.swind.fast.common.util.oConvertUtils;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.scheduling.annotation.Async;
import org.springframework.context.event.EventListener;

import org.springframework.context.ApplicationEvent;

import com.swind.fast.modules.app.space.rent.component.SpaceComponent;

import com.swind.fast.modules.app.space.rent.event.SpaceSyncAnnounceEvent;
import com.swind.fast.modules.app.space.rent.entity.SpaceContract;
import com.swind.fast.modules.app.space.rent.entity.SpaceContractBill;
import com.swind.fast.modules.app.space.rent.service.ISpaceContractBillService;
import com.swind.fast.modules.app.space.rent.model.SpaceAnnouncementModel;

import com.swind.fast.modules.system.entity.SysAnnouncement;
import com.swind.fast.modules.system.service.ISysAnnouncementService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.hutool.core.date.DateBetween;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;

import java.util.stream.Collectors;
import java.util.List;
import java.util.ArrayList;
import java.util.Date;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class SpaceSyncAnnounceListener {

    @Autowired
    private ISpaceContractBillService spaceContractBillService;

    @Autowired
    private ISysAnnouncementService sysAnnouncementService;

    @Async
    @EventListener
    public void spaceSyncAnnounceListener(SpaceSyncAnnounceEvent event) {
        log.info("spaceSyncAnnounceListener");

        // String cdate = new DateTime().toDateStr();
        // QueryWrapper<SpaceContractBill> queryWrapper = new QueryWrapper<SpaceContractBill>().select("id").lt("remind_date", cdate);
        // List<SpaceContractBill> list = spaceContractBillService.list(queryWrapper);
        // log.info("list: {}", list);

        String cdate = new DateTime().toDateStr();
        SpaceAnnouncementModel model = new SpaceAnnouncementModel();
        model.setStartDate(cdate);
        List<SpaceAnnouncementModel> list = spaceContractBillService.getSpaceAnnouncementList(model);
        // log.info("list: {}", list);
        List<String> billIds = list.stream().map(SpaceAnnouncementModel::getId).distinct().collect(Collectors.toList());
        log.info("bill ids : {}", billIds);
        String msgTitle = "房间%s, 需付款%s元.";
        String msgFormat = "<p>合同 %s, 房间 %s, 客户 %s 于 %s 需要付款 %s 元.</p>";

        DateTime edate = new DateTime();
        DateTime sdate = DateUtil.offsetDay(edate, -7);

        QueryWrapper<SysAnnouncement> queryWrapper = new QueryWrapper<SysAnnouncement>().select("msg_abstract").between("send_time", sdate, edate);
        List<SysAnnouncement> tlist = sysAnnouncementService.list(queryWrapper);
        List<String> listIds = tlist.stream().map(SysAnnouncement::getMsgAbstract).distinct().collect(Collectors.toList());
        log.info("listIds: {}", listIds);

        List<SysAnnouncement> annos = new ArrayList<SysAnnouncement>();
        for (SpaceAnnouncementModel item : list) {
            String id = item.getId();
            if (listIds.contains(id)) {
                log.info("id exists: {}", id);
                continue;
            }
            String contractNumber = item.getContractNumber();
            String room = item.getRoom();
            String customer = item.getCustomerTitle();
            String price = item.getPrice();
            String startDate = item.getStartDate();
            String msgContent = String.format(msgFormat, contractNumber, room, customer, startDate, price);
            String title = String.format(msgTitle, room, price);
            SysAnnouncement anno = SysAnnouncement.builder()
                                .titile(title)
                                .msgContent(msgContent)
                                .sender("admin")
                                // .startTime("2022-01-01 00:00:00")
                                // .endTime("2025-01-01 00:00:00")
                                .priority("M")
                                .msgCategory("1")
                                .msgType("ALL")
                                .sendStatus("1")
                                .delFlag("0")
                                .msgAbstract(id)
                                .sendTime(new Date())
                                .build();
            annos.add(anno);
        }
        sysAnnouncementService.saveBatch(annos);
    }

}
